<template>
  <div class="login-page">
    <!-- 导航栏 -->
    <header class="navbar">
      <div class="logo">
        <div class="logo-icon">A</div>
        <h1 class="logo-text">AppName</h1>
      </div>
      <nav>
        <router-link to="/register" class="nav-link">注册</router-link>
        <button @click="toggleDarkMode" class="theme-toggle" aria-label="切换主题">
          <i :class="isDarkMode ? 'fa fa-sun-o' : 'fa fa-moon-o'"></i>
        </button>
      </nav>
    </header>

    <!-- 主内容区 -->
    <main class="main-content">
      <!-- 左侧信息 -->
      <div class="info-section">
        <h2 class="info-title">欢迎回来</h2>
        <p class="info-description">请登录您的账号，继续您的旅程</p>
        <div class="features">
          <div class="feature-card">
            <i class="fa fa-shield feature-icon"></i>
            <div class="feature-info">
              <h3 class="feature-title">安全保障</h3>
              <p class="feature-desc">多重验证保护您的账号安全</p>
            </div>
          </div>
          <div class="feature-card">
            <i class="fa fa-bolt feature-icon"></i>
            <div class="feature-info">
              <h3 class="feature-title">快速访问</h3>
              <p class="feature-desc">一键登录，省时省力</p>
            </div>
          </div>
        </div>
        <div class="image-container">
          <img src="https://picsum.photos/id/180/600/400" alt="用户登录体验" class="login-image">
        </div>
      </div>

      <!-- 右侧登录表单 -->
      <div class="form-section">
        <div class="form-card">
          <h2 class="form-title">登录账号</h2>
          <p class="form-subtitle">请输入您的账号信息</p>
          
          <form @submit.prevent="handleLogin" class="login-form">
            <!-- 邮箱/用户名 -->
            <div class="form-group">
              <label for="email" class="form-label">邮箱</label>
              <div class="input-wrapper">
                <i class="fa fa-envelope input-icon"></i>
                <input
                  type="email"
                  id="email"
                  v-model="form.email"
                  @blur="validateField('email')"
                  @input="clearError('email')"
                  class="form-input"
                  placeholder="your@email.com"
                
                >
                <i v-if="errors.email" class="fa fa-exclamation-circle error-icon" aria-hidden="true"></i>
              </div>
              <p v-if="errors.email" class="error-message">{{ errors.email }}</p>
            </div>
            
            <!-- 密码 -->
            <div class="form-group">
              <div class="password-label-wrapper">
                <label for="password" class="form-label">密码</label>
                <router-link to="/forgot-password" class="forgot-link">忘记密码?</router-link>
              </div>
              <div class="input-wrapper">
                <i class="fa fa-lock input-icon"></i>
                <input
                  :type="showPassword ? 'text' : 'password'"
                  id="password"
                  v-model="form.password"
                  @blur="validateField('password')"
                  @input="clearError('password')"
                  class="form-input"
                  placeholder="请输入密码"
               
                >
                <button
                  type="button"
                  @click="showPassword = !showPassword"
                  class="toggle-password"
                  aria-label="切换密码可见性"
                >
                  <i :class="showPassword ? 'fa fa-eye-slash' : 'fa fa-eye'"></i>
                </button>
                <i v-if="errors.password" class="fa fa-exclamation-circle error-icon" aria-hidden="true"></i>
              </div>
              <p v-if="errors.password" class="error-message">{{ errors.password }}</p>
            </div>
            
            <!-- 记住我 -->
            <div class="remember-me">
              <label class="checkbox-label">
                <input
                  type="checkbox"
                  v-model="form.rememberMe"
                  class="custom-checkbox"
                >
                <span class="checkmark"></span>
                记住我
              </label>
            </div>
            
            <!-- 登录按钮 -->
            <button
              type="submit"
              :disabled="isSubmitting"
              class="login-button"
            >
              <span v-if="!isSubmitting">登录</span>
              <span v-if="isSubmitting" class="loading-indicator">
                <i class="fa fa-spinner fa-spin"></i> 登录中...
              </span>
            </button>
          </form>
          
          <!-- 分隔线 -->
          <div class="divider">
            <span class="divider-text">或使用以下方式登录</span>
          </div>
          
          <!-- 第三方登录 -->
          <div class="social-login">
            <button class="social-button google" aria-label="使用Google登录">
              <i class="fa fa-google"></i>
            </button>
            <button class="social-button facebook" aria-label="使用Facebook登录">
              <i class="fa fa-facebook"></i>
            </button>
            <button class="social-button github" aria-label="使用GitHub登录">
              <i class="fa fa-github"></i>
            </button>
          </div>
          
          <!-- 注册链接 -->
          <div class="register-prompt">
            还没有账号? <router-link to="/register" class="register-link">立即注册</router-link>
          </div>
        </div>
      </div>
    </main>
    
    <!-- 页脚 -->
    <footer class="footer">
      <div class="footer-links">
        <a href="#" class="footer-link">关于我们</a>
        <a href="#" class="footer-link">服务条款</a>
        <a href="#" class="footer-link">隐私政策</a>
        <a href="#" class="footer-link">帮助中心</a>
      </div>
      <p class="copyright">© 2023 AppName. 保留所有权利。</p>
    </footer>
    
    <!-- 登录成功提示 -->
    <div v-if="showSuccess" class="success-toast">
      <i class="fa fa-check-circle"></i>
      <span>登录成功！正在跳转...</span>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, watchEffect } from 'vue';
import { useRouter } from 'vue-router';

// 定义表单数据接口
interface LoginForm {
  email: string;
  password: string;
  rememberMe: boolean;
}

// 定义错误信息接口
interface FormErrors {
  email: string;
  password: string;
}

// 表单数据
const form: LoginForm = reactive({
  email: '',
  password: '',
  rememberMe: false
});

// 错误信息
const errors: FormErrors = reactive({
  email: '',
  password: ''
});

// 状态变量
const showPassword = ref(false);
const isSubmitting = ref(false);
const showSuccess = ref(false);
const isDarkMode = ref(false);

// 路由
const router = useRouter();

// 监听系统暗色模式偏好
watchEffect(() => {
  const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
  isDarkMode.value = prefersDark;
  document.documentElement.classList.toggle('dark', prefersDark);
});

// 切换暗色模式
const toggleDarkMode = () => {
  isDarkMode.value = !isDarkMode.value;
  document.documentElement.classList.toggle('dark', isDarkMode.value);
};

// 验证单个字段
const validateField = (field: keyof FormErrors) => {
  switch (field) {
    case 'email':
      if (!form.email.trim()) {
        errors.email = '请输入邮箱';
      } else {
        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        if (!emailRegex.test(form.email)) {
          errors.email = '请输入有效的邮箱地址';
        } else {
          errors.email = '';
        }
      }
      break;
      
    case 'password':
      if (!form.password) {
        errors.password = '请输入密码';
      } else if (form.password.length < 8) {
        errors.password = '密码长度至少为8个字符';
      } else {
        errors.password = '';
      }
      break;
  }
};

// 清除错误
const clearError = (field: keyof FormErrors) => {
  errors[field] = '';
};

// 验证整个表单
const validateForm = (): boolean => {
  let isValid = true;
  
  (Object.keys(errors) as Array<keyof FormErrors>).forEach(field => {
    validateField(field);
    if (errors[field]) {
      isValid = false;
    }
  });
  
  return isValid;
};

// 处理登录
const handleLogin = async () => {
  if (!validateForm()) {
    // 滚动到第一个错误字段
    const firstErrorField = (Object.keys(errors) as Array<keyof FormErrors>).find(field => errors[field]);
    if (firstErrorField) {
      const element = document.getElementById(firstErrorField);
      if (element) {
        element.scrollIntoView({ behavior: 'smooth', block: 'center' });
        element.focus();
      }
    }
    return;
  }
  
  isSubmitting.value = true;
  
  try {
    // 模拟API请求
    await new Promise(resolve => setTimeout(resolve, 1500));
    
    // 显示成功消息
    showSuccess.value = true;
    
    // 存储记住的用户信息（实际应用中可能使用localStorage或cookie）
    if (form.rememberMe) {
      console.log('记住用户信息:', form.email);
      // 实际应用中这里会存储用户信息
    }
    
    // 2秒后跳转到首页
    setTimeout(() => {
      router.push('/about');
    }, 2000);
    
  } catch (error) {
    console.error('登录失败:', error);
    alert('登录失败，请检查您的账号信息');
  } finally {
    isSubmitting.value = false;
  }
};
</script>

<style scoped>
/* 基础样式 */
.login-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f8fafc;
  color: #1e293b;
  transition: background-color 0.3s, color 0.3s;
}

.dark .login-page {
  background-color: #0f172a;
  color: #f1f5f9;
}

/* 导航栏样式 */
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 2rem;
}

.logo {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.logo-icon {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 0.5rem;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 1.25rem;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

.logo-text {
  font-size: 1.5rem;
  font-weight: bold;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  margin: 0;
}

nav {
  display: flex;
  align-items: center;
  gap: 1.5rem;
}

.nav-link {
  color: #6366f1;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.2s;
}

.nav-link:hover {
  color: #4f46e5;
}

.theme-toggle {
  background: none;
  border: none;
  color: #64748b;
  cursor: pointer;
  font-size: 1.25rem;
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s, color 0.2s;
}

.theme-toggle:hover {
  background-color: #e2e8f0;
  color: #334155;
}

.dark .theme-toggle:hover {
  background-color: #1e293b;
  color: #cbd5e1;
}

/* 主内容区样式 */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 2rem;
  gap: 2rem;
}

@media (min-width: 768px) {
  .main-content {
    flex-direction: row;
    align-items: center;
    justify-content: center;
    max-width: 1400px;
    margin: 0 auto;
    gap: 5rem;
  }
}

/* 左侧信息区样式 */
.info-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2rem;
  max-width: 600px;
  margin: 0 auto;
}

.info-title {
  font-size: 2.5rem;
  font-weight: bold;
  margin: 0;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.info-description {
  font-size: 1.125rem;
  color: #64748b;
  margin: 0;
}

.dark .info-description {
  color: #94a3b8;
}

.features {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.feature-card {
  display: flex;
  align-items: center;
  gap: 1rem;
  background-color: white;
  padding: 1.25rem;
  border-radius: 0.75rem;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  transition: transform 0.2s, box-shadow 0.2s;
}

.feature-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}

.dark .feature-card {
  background-color: #1e293b;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3), 0 2px 4px -1px rgba(0, 0, 0, 0.2);
}

.feature-icon {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 50%;
  background-color: #e0e7ff;
  color: #6366f1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
}

.dark .feature-icon {
  background-color: #3b82f620;
  color: #93c5fd;
}

.feature-info {
  flex: 1;
}

.feature-title {
  font-weight: 600;
  margin: 0 0 0.25rem 0;
}

.feature-desc {
  font-size: 0.875rem;
  color: #64748b;
  margin: 0;
}

.dark .feature-desc {
  color: #94a3b8;
}

.image-container {
  border-radius: 1rem;
  overflow: hidden;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s;
}

.image-container:hover {
  transform: scale(1.02);
}

.login-image {
  width: 100%;
  height: auto;
  display: block;
}

/* 右侧表单区样式 */
.form-section {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  max-width: 400px;
  margin: 0 auto;
}

.form-card {
  width: 100%;
  background-color: white;
  border-radius: 1rem;
  padding: 2.5rem;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
  transition: box-shadow 0.3s;
}

.form-card:hover {
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
}

.dark .form-card {
  background-color: #1e293b;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3), 0 4px 6px -2px rgba(0, 0, 0, 0.2);
}

.form-title {
  font-size: 1.75rem;
  font-weight: bold;
  margin: 0 0 0.5rem 0;
  text-align: center;
}

.form-subtitle {
  color: #64748b;
  text-align: center;
  margin: 0 0 2rem 0;
}

.dark .form-subtitle {
  color: #94a3b8;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.form-label {
  font-weight: 500;
  font-size: 0.875rem;
}

.password-label-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.forgot-link {
  font-size: 0.875rem;
  color: #6366f1;
  text-decoration: none;
  transition: color 0.2s;
}

.forgot-link:hover {
  color: #4f46e5;
  text-decoration: underline;
}

.input-wrapper {
  position: relative;
}

.form-input {
  width: 100%;
  padding: 1rem 1rem 1rem 2.5rem;
  border: 1px solid #cbd5e1;
  border-radius: 0.5rem;
  font-size: 1rem;
  transition: border-color 0.2s, box-shadow 0.2s;
  background-color: transparent;
  color: inherit;
}

.form-input:focus {
  outline: none;
  border-color: #6366f1;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
}

.dark .form-input {
  border-color: #334155;
}

.dark .form-input:focus {
  border-color: #818cf8;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.3);
}

.input-icon {
  position: absolute;
  left: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  color: #94a3b8;
}

.toggle-password {
  position: absolute;
  right: 2.5rem;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: #94a3b8;
  cursor: pointer;
  transition: color 0.2s;
}

.toggle-password:hover {
  color: #64748b;
}

.error-icon {
  position: absolute;
  right: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  color: #ef4444;
}

.error-message {
  color: #ef4444;
  font-size: 0.875rem;
  margin: 0;
  animation: shake 0.5s ease-in-out;
}

/* 记住我样式 */
.remember-me {
  margin-top: 0.5rem;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  font-size: 0.875rem;
  user-select: none;
}

.custom-checkbox {
  appearance: none;
  width: 1rem;
  height: 1rem;
  border: 1px solid #cbd5e1;
  border-radius: 0.25rem;
  position: relative;
  cursor: pointer;
  transition: background-color 0.2s, border-color 0.2s;
}

.custom-checkbox:checked {
  background-color: #6366f1;
  border-color: #6366f1;
}

.custom-checkbox:checked::after {
  content: '✓';
  position: absolute;
  color: white;
  font-size: 0.75rem;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.dark .custom-checkbox {
  border-color: #334155;
}

.checkmark {
  display: inline-block;
}

/* 登录按钮样式 */
.login-button {
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  color: white;
  border: none;
  border-radius: 0.5rem;
  padding: 1rem;
  font-weight: 600;
  font-size: 1rem;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s, opacity 0.2s;
  margin-top: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}

.login-button:hover {
  box-shadow: 0 4px 6px -1px rgba(99, 102, 241, 0.4);
  transform: translateY(-2px);
}

.login-button:active {
  transform: translateY(0);
}

.login-button:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.loading-indicator {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

/* 分隔线样式 */
.divider {
  position: relative;
  height: 1px;
  background-color: #e2e8f0;
  margin: 2rem 0;
}

.dark .divider {
  background-color: #334155;
}

.divider-text {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background-color: white;
  padding: 0 1rem;
  font-size: 0.875rem;
  color: #64748b;
}

.dark .divider-text {
  background-color: #1e293b;
  color: #94a3b8;
}

/* 第三方登录样式 */
.social-login {
  display: flex;
  gap: 1rem;
  justify-content: center;
  margin-bottom: 2rem;
}

.social-button {
  width: 3rem;
  height: 3rem;
  border-radius: 50%;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
}

.social-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

.social-button:active {
  transform: translateY(0);
}

.google {
  background-color: #f8fafc;
  color: #ea4335;
  border: 1px solid #e2e8f0;
}

.dark .google {
  background-color: #334155;
  border-color: #475569;
}

.facebook {
  background-color: #f8fafc;
  color: #1877f2;
  border: 1px solid #e2e8f0;
}

.dark .facebook {
  background-color: #334155;
  border-color: #475569;
}

.github {
  background-color: #f8fafc;
  color: #333;
  border: 1px solid #e2e8f0;
}

.dark .github {
  background-color: #334155;
  border-color: #475569;
  color: #f1f5f9;
}

/* 注册提示样式 */
.register-prompt {
  text-align: center;
  font-size: 0.875rem;
  color: #64748b;
}

.dark .register-prompt {
  color: #94a3b8;
}

.register-link {
  color: #6366f1;
  font-weight: 500;
  text-decoration: none;
  transition: color 0.2s;
}

.register-link:hover {
  color: #4f46e5;
  text-decoration: underline;
}

/* 页脚样式 */
.footer {
  padding: 2rem;
  text-align: center;
  border-top: 1px solid #e2e8f0;
}

.dark .footer {
  border-top-color: #334155;
}

.footer-links {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 1.5rem;
  margin-bottom: 1rem;
}

.footer-link {
  color: #64748b;
  text-decoration: none;
  font-size: 0.875rem;
  transition: color 0.2s;
}

.footer-link:hover {
  color: #6366f1;
  text-decoration: underline;
}

.copyright {
  color: #94a3b8;
  font-size: 0.75rem;
  margin: 0;
}

/* 成功提示样式 */
.success-toast {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  background-color: #10b981;
  color: white;
  padding: 1rem 1.5rem;
  border-radius: 0.5rem;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  z-index: 1000;
  animation: slideIn 0.3s ease-out forwards, fadeOut 0.3s ease-in forwards 2.7s;
}

/* 动画 */
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }
  20%, 40%, 60%, 80% { transform: translateX(5px); }
}

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

@keyframes fadeOut {
  from { transform: translateX(0); opacity: 1; }
  to { transform: translateX(100%); opacity: 0; }
}
</style>